package com.huahua.api.controller.ai;

import com.huahua.api.entity.base.R;
import com.huahua.api.entity.param.TtsCompleteParam;
import com.huahua.api.entity.param.TtsCreateParam;
import com.huahua.api.entity.vo.TtsRecordVo;
import com.huahua.api.service.TtsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author Huahua-Ai
 * @date 2025-01-10 15:10:00
 * @description TTS文字转语音控制器
 */
@RestController
@RequestMapping("/tts")
public class TtsController {

    private static final Logger log = LoggerFactory.getLogger(TtsController.class);

    @Autowired
    private TtsService ttsService;

    @PostMapping("/task/create")
    public Mono<R<TtsRecordVo>> createTask(@RequestBody TtsCreateParam param) {
        log.info("创建TTS任务，文字长度: {}",
                param.getTextContent() != null ? param.getTextContent().length() : 0);

        return ttsService.createTask(param)
                .map(R::success)
                .onErrorResume(e -> {
                    log.error("创建TTS任务失败", e);
                    return Mono.just(R.error(e.getMessage()));
                });
    }

    @GetMapping("/task/status/{id}")
    public Mono<R<TtsRecordVo>> getTaskStatus(@PathVariable Long id) {
        log.info("获取TTS任务状态，ID: {}", id);

        return ttsService.getTaskStatus(id)
                .map(R::success)
                .onErrorResume(e -> {
                    log.error("获取TTS任务状态失败，ID: {}", id, e);
                    return Mono.just(R.error(e.getMessage()));
                });
    }

    @PostMapping("/task/complete")
    public Mono<R<TtsRecordVo>> completeTask(@RequestBody TtsCompleteParam param) {
        log.info("TTS任务完成回调，记录ID: {}, 成功: {}",
                param.getRecordId(), param.getSuccess());

        return ttsService.completeTask(param)
                .map(R::success)
                .onErrorResume(e -> {
                    log.error("处理TTS任务完成回调失败，记录ID: {}", param.getRecordId(), e);
                    return Mono.just(R.error(e.getMessage()));
                });
    }

    @GetMapping("/task/pending")
    public Mono<R<List<TtsRecordVo>>> getPendingTasks() {
        log.info("获取待开始的TTS任务列表");

        return ttsService.getPendingTasks()
                .collectList()
                .map(R::success)
                .onErrorResume(e -> {
                    log.error("获取待开始任务列表失败", e);
                    return Mono.just(R.error(e.getMessage()));
                });
    }

    @PostMapping("/task/start/{id}")
    public Mono<R<TtsRecordVo>> startTask(@PathVariable Long id) {
        log.info("开始执行TTS任务，ID: {}", id);

        return ttsService.startTask(id)
                .map(R::success)
                .onErrorResume(e -> {
                    log.error("开始执行任务失败，ID: {}", id, e);
                    return Mono.just(R.error(e.getMessage()));
                });
    }
}
